package com.java110.mall.openapi.bmo.impl;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.dto.file.FileRelDto;
import com.java110.dto.housekeeping.HousekeepingServDto;
import com.java110.dto.product.ProductDetailDto;
import com.java110.dto.product.ProductServKeywordDto;
import com.java110.dto.store.StoreDto;
import com.java110.intf.cart.IHousekeepingServFeeInnerServiceSMO;
import com.java110.intf.goods.IHousekeepingServInnerServiceSMO;
import com.java110.intf.goods.IProductDetailInnerServiceSMO;
import com.java110.intf.goods.IProductServKeywordInnerServiceSMO;
import com.java110.intf.system.IFileInnerServiceSMO;
import com.java110.intf.system.IFileRelInnerServiceSMO;
import com.java110.mall.openapi.bmo.IMallCommonApiBmo;
import com.java110.utils.cache.MappingCache;
import com.java110.utils.constant.MappingConstant;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.*;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("queryHousekeepingServBmoImpl")
public class QueryHousekeepingServBmoImpl  implements IMallCommonApiBmo {

    @Autowired
    private IHousekeepingServInnerServiceSMO housekeepingServInnerServiceSMOImpl;
    @Autowired
    private IFileInnerServiceSMO fileInnerServiceSMOImpl;
    @Autowired
    private IFileRelInnerServiceSMO fileRelInnerServiceSMOImpl;
    @Autowired
    private IProductServKeywordInnerServiceSMO productServKeywordInnerServiceSMOImpl;
    @Autowired
    private IProductDetailInnerServiceSMO productDetailInnerServiceSMOImpl;
    @Autowired
    private IHousekeepingServFeeInnerServiceSMO housekeepingServFeeInnerServiceSMOImpl;

    @Override
    public void validate(ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.jsonObjectHaveKey(reqJson, "page", "请求中未包含page信息");
        Assert.jsonObjectHaveKey(reqJson, "row", "请求中未包含row信息");
        Assert.jsonObjectHaveKey(reqJson, "adminStoreTypeCd", "请求中未包含adminStoreTypeCd信息");

        String adminStoreTypeCd = reqJson.getString("adminStoreTypeCd");
        if (!StoreDto.STORE_TYPE_ADMIN.equals(adminStoreTypeCd)) {
            throw new CmdException("身份不合法");
        }
        reqJson.remove("storeId");
    }

    @Override
    public void doCmd(ICmdDataFlowContext context, JSONObject reqJson) {

        HousekeepingServDto housekeepingServDto = BeanConvertUtil.covertBean(reqJson, HousekeepingServDto.class);
        String createTime = reqJson.getString("createTime");

        if (null != createTime && !"".equals(createTime)) {
            housekeepingServDto.setCreateTime(DateUtil.getDateFromStringB(createTime));
        }

        int count = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);

        List<HousekeepingServDto> housekeepingServDtos = null;
        if (count > 0) {
            housekeepingServDtos = housekeepingServInnerServiceSMOImpl.queryHousekeepingServs(housekeepingServDto);
        } else {
            housekeepingServDtos = new ArrayList<>();
        }
        if (!ListUtil.isNull(housekeepingServDtos)) {

            String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");

            imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");

            for (HousekeepingServDto housekeepingServ : housekeepingServDtos) {
                //查询服务默认费用
                housekeepingServ.setDefaultFee(housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServ.getShopId(), housekeepingServ.getServId()));

                //服务详情
                ProductDetailDto productDetailDto = new ProductDetailDto();
                productDetailDto.setShopId(housekeepingServ.getShopId());
                productDetailDto.setProductId(housekeepingServ.getServId());
                List<ProductDetailDto> productDetailDtos = productDetailInnerServiceSMOImpl.queryProductDetails(productDetailDto);
                if (null != productDetailDtos && productDetailDtos.size() > 0) {
                    ProductDetailDto productDetail = productDetailDtos.get(0);
                    housekeepingServ.setContext(productDetail.getContent());
                }
                String coverPhoto = doGetRentingFile(housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_COVER);
                if(!StringUtil.isEmpty(coverPhoto) && !coverPhoto.startsWith("http")){
                    coverPhoto = imgUrl  +coverPhoto;
                }
                //封面图
                housekeepingServ.setCoverPhoto(coverPhoto);

                //轮播图
                housekeepingServ.setCarouselFigurePhoto(doGetRentingFiles(imgUrl, housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE));
                //关键字
                housekeepingServ.setKeyword(doGetkeywords(housekeepingServ));
            }
        }

        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServDto.getRow()), count, housekeepingServDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);

    }


    /**
     * @param housekeepingServDto
     * @return 订单服务能够接受的报文
     */
    public ResponseEntity<String> getSearch(HousekeepingServDto housekeepingServDto) {
        int count = housekeepingServInnerServiceSMOImpl.queryHousekeepingServsCount(housekeepingServDto);
        List<HousekeepingServDto> housekeepingServDtos = null;
        if (count > 0) {
            housekeepingServDtos = housekeepingServInnerServiceSMOImpl.searchHousekeepingServs(housekeepingServDto);
        } else {
            housekeepingServDtos = new ArrayList<>();
        }
        if (null != housekeepingServDtos && housekeepingServDtos.size() > 0) {
            String imgUrl = MappingCache.getValue(MappingConstant.FILE_DOMAIN,"IMG_PATH");
            imgUrl += (!StringUtil.isEmpty(imgUrl) && imgUrl.endsWith("/") ? "" : "/");
            for (HousekeepingServDto housekeepingServ : housekeepingServDtos) {
                //查询服务默认费用
                housekeepingServ.setDefaultFee(housekeepingServFeeInnerServiceSMOImpl.queryHousekeepingServDefaultFee(housekeepingServ.getShopId(), housekeepingServ.getServId()));
                //服务详情
                ProductDetailDto productDetailDto = new ProductDetailDto();
                productDetailDto.setShopId(housekeepingServ.getShopId());
                productDetailDto.setProductId(housekeepingServ.getServId());
                List<ProductDetailDto> productDetailDtos = productDetailInnerServiceSMOImpl.queryProductDetails(productDetailDto);
                if (null != productDetailDtos && productDetailDtos.size() > 0) {
                    ProductDetailDto productDetail = productDetailDtos.get(0);
                    housekeepingServ.setContext(productDetail.getContent());
                }
                String coverPhoto = doGetRentingFile(housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_COVER);
                if(!StringUtil.isEmpty(coverPhoto)){
                    coverPhoto = imgUrl  +coverPhoto;
                }
                //封面图
                housekeepingServ.setCoverPhoto(coverPhoto);
                //轮播图
                //housekeepingServ.setCarouselFigurePhoto(doGetRentingFiles(imgUrl, housekeepingServ, FileRelDto.REL_TYPE_CD_GOODS_CAROUSEL_FIGURE));
                //关键字
                //housekeepingServ.setKeyword(doGetkeywords(housekeepingServ));
            }
        }
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) housekeepingServDto.getRow()), count, housekeepingServDtos);
        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);
        return responseEntity;
    }




    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private String doGetRentingFile(HousekeepingServDto housekeepingServDto, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(housekeepingServDto.getServId());
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        FileRelDto fileRel = null;
        if (null != fileRelDtos && fileRelDtos.size() > 0) {
            fileRel = fileRelDtos.get(0);
            return fileRel.getFileSaveName();
        }
        return "";
    }

    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private List<String> doGetRentingFiles(String imgUrl, HousekeepingServDto housekeepingServDto, String fileTypeCd) {

        FileRelDto fileRelDto = new FileRelDto();
        fileRelDto.setObjId(housekeepingServDto.getServId());
        fileRelDto.setRelTypeCd(fileTypeCd);
        List<FileRelDto> fileRelDtos = fileRelInnerServiceSMOImpl.queryFileRels(fileRelDto);
        List<String> fileRelName = new ArrayList<String>();
        if (null != fileRelDtos && fileRelDtos.size() > 0) {
            for (FileRelDto filerels : fileRelDtos) {
                if(StringUtil.isEmpty(filerels.getFileSaveName())){
                    continue;
                }
                if(filerels.getFileSaveName().startsWith("http")){
                    fileRelName.add( filerels.getFileSaveName());
                    continue;
                }
                fileRelName.add(imgUrl + filerels.getFileSaveName());
            }
        }
        return fileRelName;
    }

    /**
     * 保存房屋租赁 图片
     *
     * @param housekeepingServDto
     */
    private String doGetkeywords(HousekeepingServDto housekeepingServDto) {

        ProductServKeywordDto servKeywordDtos = new ProductServKeywordDto();
        servKeywordDtos.setObjId(housekeepingServDto.getServId());
        servKeywordDtos.setShopId(housekeepingServDto.getShopId());
        String keywords = "";
        List<ProductServKeywordDto> serkeywords = productServKeywordInnerServiceSMOImpl.queryProductServKeywords(servKeywordDtos);
        if (null != serkeywords && serkeywords.size() > 0) {
            for (ProductServKeywordDto servKeywordDto : serkeywords) {
                keywords = keywords + ";" + servKeywordDto.getKeywordName();
            }
            keywords = keywords.substring(1, keywords.length());
        }

        return keywords;
    }

}
